package com.cn.demo.redisson;

import org.junit.Test;
import org.redisson.Redisson;
import org.redisson.api.RList;
import org.redisson.api.RScript;
import org.redisson.api.RedissonClient;
import org.redisson.client.codec.IntegerCodec;
import org.redisson.client.codec.LongCodec;
import org.redisson.client.codec.StringCodec;
import org.redisson.config.Config;

import javax.print.DocFlavor;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * 描述
 *
 * @author wangjf
 * @version 1.0
 * @createDate 2019/12/17 14:53
 * @projectName demo
 */
public class RedissonLua {

    public RedissonClient getRedisson(){
        Config config = new Config();
        config.useSingleServer()
                .setTimeout(1000000)
                .setAddress("redis://192.168.199.32:16380")
                .setPassword("123456");

        RedissonClient redisson =Redisson.create(config);

        return redisson;
    }

    @Test
    public void test0(){

        RedissonClient redisson = getRedisson();
        RList<String> list_test = redisson.getList("list_test");
        list_test.add("aaa");
        list_test.add("bbb");
        System.out.println(list_test.size());

    }

    @Test
    public void testReturn(){

        RedissonClient redisson = getRedisson();
        String lua = "return KEYS[1]";
        List<Object> list = new ArrayList<>();
        list.add("aaa");
        String eval = redisson.getScript(StringCodec.INSTANCE).eval(RScript.Mode.READ_ONLY, lua, RScript.ReturnType.VALUE, list, Collections.emptyList());
        System.out.println(eval);
    }

    @Test
    public void testSet(){

        RedissonClient redisson = getRedisson();
        String lua = "return redis.call('set',KEYS[1],ARGV[1])";
        List<Object> list = new ArrayList<>();
        list.add("mytest");
        String eval = redisson.getScript(StringCodec.INSTANCE).eval(RScript.Mode.READ_WRITE, lua, RScript.ReturnType.STATUS, list, "test");
        System.out.println(eval);

    }

    @Test
    public void testGet(){

        RedissonClient redisson = getRedisson();
        String lua = "return redis.call('get',KEYS[1])";
        List<Object> list = new ArrayList<>();
        list.add("mytest");
        String eval = redisson.getScript(StringCodec.INSTANCE).eval(RScript.Mode.READ_ONLY, lua, RScript.ReturnType.VALUE, list, Collections.emptyList());
        System.out.println(eval);

    }

    @Test
    public void testDel(){

        RedissonClient redisson = getRedisson();
        String lua = "return redis.call('del',KEYS[1])";
        List<Object> list = new ArrayList<>();
        list.add("mytest");
        Long eval = redisson.getScript(LongCodec.INSTANCE).eval(RScript.Mode.READ_WRITE, lua, RScript.ReturnType.VALUE, list, Collections.emptyList());
        System.out.println(eval);


    }




}
